package dataview;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.nio.charset.Charset;

public class EasyTextFile extends EasyFile {
	private static final long serialVersionUID = 4911804024662783668L;
	private String text;
	
	public EasyTextFile(String path, String text) {
		super(path);
		this.text = text;
	}
	
	public EasyTextFile(File file) {
		super(file.getPath());
		try {
			FileInputStream fis = new FileInputStream(this);
			text = "";
			int c;
			while ((c = fis.read()) != -1)
				text += (char)c;
		} catch (Exception e) {
			text = null;
		}
	}
	
	public void print(String path) throws IOException {
		OutputStreamWriter osw = null;
		Writer w = null;
		if (!path.endsWith(".ecd")) path = path + ".ecd";
		osw = new OutputStreamWriter(new FileOutputStream(path), Charset.forName("UTF-8"));
		w = osw.append(text);
		w.close();
	}
	
	public String getText() {
		return text;
	}
}
